import 'package:lighthouse/pages/function.dart';
import 'package:lighthouse/pages/home.dart';
import 'package:lighthouse/pages/map.dart';
import 'package:lighthouse/pages/me.dart';
import 'package:dio/dio.dart';

import '../index.dart';

class StartPage extends StatefulWidget {
  const StartPage({Key? key}) : super(key: key);

  @override
  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> {
  int _selectedIndex = 0;
  final List<Widget> _pages = <Widget>[];
  final List<BottomNavigationBarItem> _items = <BottomNavigationBarItem>[];
  late PageController controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller = PageController(initialPage: 0);
    //更新缓存数据
    loadConfigureData();
  }

  void loadConfigureData() async {
    DataCache.renewCacheData(context);
  }

  @override
  Widget build(BuildContext context) {
    _pages.clear();
    _items.clear();
    if (PermissionCache().getPermission("home") != null) {
      _pages.add(const HomePage());
      _items.add(BottomNavigationBarItem(
        icon: Image.asset(
          'images/bottom_home.png',
        ),
        activeIcon: Image.asset(
          'images/bottom_home_s.png',
        ),
        label: '首页',
      ));
    }
    if (PermissionCache().getPermission("function") != null) {
      _pages.add(const FunctionPage());
      _items.add(BottomNavigationBarItem(
        icon: Image.asset(
          'images/bottom_features.png',
        ),
        activeIcon: Image.asset(
          'images/bottom_features_s.png',
        ),
        label: '功能',
      ));
    }
    // if (PermissionCache().getPermission("map") != null) {
    //   _pages.add(const MapPage());
    //   _items.add(
    //     BottomNavigationBarItem(
    //       icon: Image.asset(
    //         'images/bottom/map.png',
    //         height: 24,
    //       ),
    //       activeIcon: Image.asset(
    //         'images/bottom/map_s.png',
    //         height: 24,
    //       ),
    //       label: '地图',
    //     ),
    //   );
    // }
    if (_pages.isEmpty) {
      //直接显示我的
      return const Scaffold(
        body: MePage(), // 构建主页面
      );
    } else {
      _pages.add(const MePage());
      _items.add(
        BottomNavigationBarItem(
          icon: Image.asset(
            'images/bottom_me.png',
          ),
          activeIcon: Image.asset(
            'images/bottom_me_s.png',
          ),
          label: '我的',
        ),
      );
    }

    return Scaffold(
      body: _buildBody(), // 构建主页面
      // drawer: const MyDrawer(), //抽屉菜单
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  void _onItemTapped(int index) {
    if (index != _selectedIndex) {
      setState(() {
        _selectedIndex = index;
        controller.animateToPage(
          _selectedIndex,
          duration: const Duration(milliseconds: 300),
          curve: Curves.easeInCubic,
        );
      });
    }
  }

  Widget? _buildBottomNavigationBar() {
    UserModel userModel = Provider.of<UserModel>(context);
    if (userModel.isLogin) {
      return BottomNavigationBar(
        backgroundColor: Theme.of(context).snackBarTheme.backgroundColor,
        items: _items,
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        selectedItemColor: Theme.of(context).colorScheme.primary,
        unselectedItemColor: Theme.of(context).unselectedWidgetColor,
        showUnselectedLabels: true,
      );
    } else {
      return null;
    }
  }

  Widget? _buildBody() {
    UserModel userModel = Provider.of<UserModel>(context);
    if (!userModel.isLogin) {
      //用户未登录，显示登录按钮
      return Center(
        child: ElevatedButton(
          child: Text(
            S.of(context).login,
          ),
          onPressed: () => Navigator.of(context).pushNamed("login"),
        ),
      );
    } else {
      return PageView(
          controller: controller,
          children: _pages,
          allowImplicitScrolling: true,
          //allowImplicitScrolling 为 true 时会缓存前后各一页
          onPageChanged: (int index) {
            if (index != _selectedIndex) {
              setState(() {
                _selectedIndex = index;
              });
            }
          });
      //已登录，则显示项目列表
    }
  }
}
